package com.shop.manager.controller;


//import com.shop.manager.entity.ContentCategory;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.ContentCategory;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.ContentCategoryService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 分类信息增删改查
 */

@RestController
@RequestMapping("/categorys")
public class ContentCategoryController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private ContentCategoryService contentCategoryService;

    /**
     * @Description 添加分类信息
     * @param contentCategory 分类对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "ContentCategory 对象") ContentCategory contentCategory){
        try {
            contentCategoryService.add(contentCategory);
            return HttpResult.ok("添加成功!");
        } catch (Exception e){
            e.printStackTrace();
        }
        return HttpResult.error("添加失败!");
    }

    /**
     * @Description 批量删除分类信息
     * @param ids 分类id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Integer[] ids){
        try {
            contentCategoryService.deleteByIds(ids);
            return HttpResult.ok("删除成功!");
        } catch (Exception e){
            e.printStackTrace();
        }
        return HttpResult.error("删除失败!");
    }

    /**
     * @Description 修改分类信息
     * @param contentCategory 修改的分类对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody ContentCategory contentCategory){
        try {
            contentCategoryService.update(contentCategory);
            return HttpResult.ok("修改成功!");
        } catch (Exception e){
            e.printStackTrace();
        }
        return HttpResult.error("修改失败!");
    }

    /**
     * @Description 根据id获取城市信息
     * @param id 分类id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Integer id){
        return HttpResult.ok(contentCategoryService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param contentCategory 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) ContentCategory contentCategory){
        try {
            //分页查询
            PageResult result = contentCategoryService.findPage(pageNum,size,contentCategory);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询分类异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = contentCategoryService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询分类信息
     * @param contentCategory 分类对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "ContentCategory 对象") ContentCategory contentCategory) {
        return HttpResult.ok(contentCategoryService.findByWhere(contentCategory));
    }

    /**
     * @Description 查询全部分类信息
     * @return
     */
    @RequestMapping("/all")
    public HttpResult list(){
        return contentCategoryService.findAll();
    }





}
